import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/cupertino.dart' hide Action;
import 'package:www.znpigai.teacher/utils/nav.dart';
import 'package:www.znpigai.teacher/utils/util.dart';

import 'action.dart';
import 'state.dart';

Effect<PracticeState> buildEffect() {
  return combineEffects(<Object, Effect<PracticeState>>{
    PracticeAction.onClick: _onClick,
  });
}

void _onClick(Action action, Context<PracticeState> ctx) async {
  var practiceId = ctx.state.id;

  if (!Util.canUseAi(ctx.state.userInfo)) {
    bool ok =
        await Util.showConfirmDialog('会员已过期', '您的会员已过期，点击[确认]去充值', ctx.context);
    if (ok == true) {
      // 跳转去支付
      Navigator.of(ctx.context).pushNamed(Nav.PAY);
    }
    return;
  }

  var done =
      await Navigator.of(ctx.context).pushNamed(Nav.WEB_PAGE, arguments: {
    'url': '/correct/pub/view/machine/review.mvc?id=$practiceId&ishomework=0',
    'share': true,
    'done': '去修改'
  });

  if (done != null) {
    Navigator.of(ctx.context).pushNamed(Nav.PRACTICE_EDIT, arguments: {
      'id': ctx.state.id,
      'isHomework': false,
      'type': ctx.state.articleType,
      'grade': ctx.state.grade,
    });
  }
}
